package com.ziyear.servlet;

import com.ziyear.domain.Member;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 功能描述 : 校验会员是否登录
 *
 * @author Ziyear
 */
public class CheckLoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Member member = (Member) session.getAttribute("member");
        String temp = request.getParameter("temp");
        String seatType = request.getParameter("seatType");
        boolean waiting = Boolean.parseBoolean(request.getParameter("waiting"));
        // 判断是否已经登录
        if (member != null) {
            // 登陆了
            if ("1".equals(temp)) {
                // 跳转到会员中心界面
                response.sendRedirect("pages/user/myCenter/myCenter.jsp");
            }

            if ("2".equals(temp)) {
                // 跳转到订票的Servlet
                Integer trainId = Integer.valueOf(request.getParameter("trainId"));
                request.setAttribute("trainId", trainId);
                request.setAttribute("seatType", seatType);
                request.setAttribute("waiting", waiting);
                request.getRequestDispatcher("BookingServlet").forward(request, response);
            }
        } else {
            // 没有登录，跳转到会员登录界面
            PrintWriter out = response.getWriter();
            out.write("<script>alert('您还没有登录，请先登录！');window.location.href='pages/user/memberLogin.jsp'</script>");
            out.close();
        }
    }

}
